#!/bin/sh

# These definitions must match the definitions in sis1100_var.h
MINORBITS=8
MINORCARDBITS=2
MINORTYPEBITS=2
MINORUSERBITS=`expr $MINORBITS - $MINORCARDBITS - $MINORTYPEBITS`
# USERBITS overwrites MINORUSERBITS
USERBITS=1

# minornumber: ccttuuuu

saved_ifs=$IFS
IFS='. '
read kern_a kern_b kern_c << END
`uname -r`
END
IFS=$saved_ifs

modulename=sis1100

if [ $kern_b -gt 4 ]; then
    module=$modulename.ko
else
    module=$modulename
fi

basename=${modulename}_
devdir=/dev
mode=666
owner=root
group=root

types0="remote"
types1="ram"
types2="ctrl"
types3="dsp"

mkname () {
    card=$1
    type=$2
    user=$3
    tname=`eval echo \\\$types$type`
    name=$devdir/${basename}`printf '%d%x' $card $user`$tname
    echo $name
}

shiftv () {
    val=$1
    i=$2
    while [ $i -gt 0 ]; do
        val=`expr $val \* 2`
        i=`expr $i - 1`
    done
    echo $val
}

MAXCARDS=`shiftv 1 $MINORCARDBITS`
#echo MAXCARDS $MAXCARDS
CARDSHIFT=`expr $MINORUSERBITS + $MINORTYPEBITS`
TYPESHIFT=$MINORUSERBITS
MAXTYPES=`shiftv 1 $MINORTYPEBITS`
#echo MAXTYPES $MAXTYPES
MAXUSER=`shiftv 1 $USERBITS`
#echo MAXUSER $MAXUSER

if grep -q $modulename /proc/devices; then
    echo $modulename already loaded!
else
    # invoke insmod or modpobe with all arguments we got
    if [ -f $module ]; then
        insmod $module $* || exit 1
    else
        modprobe $modulename $* || exit 1
    fi
fi

major=`cat /proc/devices | awk "\\$2==\"$modulename\" {print \\$1}"`
echo major $device: $major

rm -f $devdir/${basename}*

card=`expr $MAXCARDS - 1`
while [ $card -ge 0 ]; do
    #echo card $card
    type=`expr $MAXTYPES - 1`
        while [ $type -ge 0 ]; do
            #echo "  " type $type
            user=`expr $MAXUSER - 1`
            while [ $user -ge 0 ]; do
                echo "    " user $user
                cardbits=`shiftv $card $CARDSHIFT`
                typebits=`shiftv $type $TYPESHIFT`
                minorbits=`expr $cardbits + $typebits + $user`
                #echo "      " bits `printf '%x' $minorbits`
                name=`mkname $card $type $user`
                echo mknod $name c $major $minorbits
                mknod $name c $major $minorbits
                user=`expr $user - 1`
            done
            type=`expr $type - 1`
        done
    card=`expr $card - 1`
done

chown $owner $devdir/${basename}*
chgrp $group $devdir/${basename}*
chmod $mode $devdir/${basename}*
